// packageA/pages/cluster/clusterRecord.js
const app = getApp();
import CommonService from '../../../utils/service/commonService.js';
import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast';
const util = require('../../../utils/util');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //列表
    list:[],
    //详情
    detailList:[],
    //无数据
    showNothing:false,
    //折叠面板当前值
    activeName: null,
    //日期
    showDate:false,
    currentDate: new Date().getTime(),
    maxDate: new Date().getTime(),
    currentDateStr: util.timeFormatDay(new Date()),
    //明天
    tomorrow:null,
    //权限
    authority: null,
    //项目选择框
    showProject:false,
    //项目选择数据
    projectList:[],
    //项目id
    projectId:null,
    //项目名称
    projectName:'选择部门',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({   
      authority: app.globalData.authority  
    })

    //查询项目
    this.getProjectList(true);
  },

  //获取列表
  getList(){
    Toast.loading({
      duration: 0,
      forbidClick: true,
      message: '加载中...'
    });

    //明天
    let t=util.timeFormatDay(util.countNextDay(new Date(this.data.currentDate),1));
    this.setData({
      tomorrow: t
    })

    let param={
      startTime: this.data.currentDateStr,
      endTime: t,
      projectId: this.data.projectId
    }

    //员工查询
    let authority = this.data.authority;
    if(authority.manage.userCard.staff){
      let user = app.globalData.userInfo;
      param.imei = user.imei ? user.imei : null;
    }  

    CommonService.juGangRecord(param).then(res=>{
      Toast.clear();
      if(res.data.data && res.data.data.result && res.data.data.result.length){
        let result=res.data.data.result;

        result.forEach(detail => {
          detail.forEach(item => {
            if(item.inTime && item.inTime!='0001-01-01T00:00:00Z'){
              item.self_inTime = item.inTime.substring(11,19);
            }
  
            if(item.outTime && item.outTime!='0001-01-01T00:00:00Z'){
              item.self_outTime = item.outTime.substring(11,19);
            }
          })
        });

        console.log('记录');
        console.log(result);
        this.setData({
          list: result
        })
      }else{
        this.setData({
          showNothing: true
        })
      }
    }).catch((error) => {
      this.setData({
        showNothing: true
      })
    })
  },

  //切换
  onChange(event) {
    this.setData({
      activeName: event.detail,
    })
  }, 

  //展开显示详情
  showDetail(event){
    let param={
      imei: event.detail,
      startTime: this.data.currentDateStr,
      endTime: this.data.tomorrow
    }

    CommonService.jobWaitingRecord(param).then(res=>{
      Toast.clear();
      if(res.data.data && res.data.data.result && res.data.data.result.length){
        let result=res.data.data.result;

        console.log('详情');
        console.log(result);

        result.forEach(item => {
          if(item.inTime && item.inTime!='0001-01-01T00:00:00Z'){
            item.self_inTime = item.inTime.substring(11,19);
          }

          if(item.outTime && item.outTime!='0001-01-01T00:00:00Z'){
            item.self_outTime = item.outTime.substring(11,19);
          }

          if(item.intervals){
            item.self_intervals=util.secondToFormat(item.intervals/1000);
          }  
          
        });

        this.setData({
          detailList: result
        })
      }
    }).catch((error) => {
    })
  },

  //刷新列表
  refreshListAndPage(){
    this.setData({
      list: [],
      showNothing: false
    })

    this.getList();  
  },

  //显示日期
  onShowDate(){
    this.setData({
			showDate: true
    })
  },

  //隐藏日期
  onCloseDate(){
    this.setData({
			showDate: false
    })
  },

  //确认日期
  onConfirmDate(e){
    this.setData({
      currentDate: e.detail,
      currentDateStr: util.timeFormatDay(e.detail)
    })

    this.onCloseDate();

    //获取列表
    this.refreshListAndPage();
  },


  //显示项目
  onShowProject(){
    this.setData({
			showProject: true
    })
  },

  //查询项目
  getProjectList(flag){
    this.setData({
      projectList: []
    })

    let param = {
      isAuth: true,
      className: 'depart',
      data: {
        searchAll: true,
        selectFields: ['id','name'],
        isProject: true
      }
    }

    //模糊查询
    if(this.data.valueSearchProject){
      param.data.name='%' + this.data.valueSearchProject + '%';
    }

    CommonService.goFindList(param).then(res=>{
      if(res.data.data && res.data.data.result && res.data.data.result.length){
        let result=res.data.data.result;
        let arr=[];

        result.forEach(item => {
          arr.push({
            text:item.name,
            id:item.id
          });
        });

        this.setData({
          projectList: arr,
          projectId: result[0].id,
          projectName: result[0].name
        })

        if(flag){
          //获取列表
          this.getList();
        }
       
      }
    }).catch((error) => {
    })
  },

  //隐藏项目
  onCloseProject(){
    this.setData({
      showProject: false,
      valueSearchProject: ''
    })
  },

  //确认项目
  onConfirmProject(e){
    this.setData({
      projectId:e.detail.value.id,
      projectName:e.detail.value.text
    })

    this.onCloseProject();

    this.setData({
      list: [],
      showNothing: false
    })
    this.getList();
  },

  //搜索项目
  onChangeProjectVal(e){
    this.setData({
      valueSearchProject: e.detail
    })
    this.getProjectList(false);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})